﻿using Microsoft.AspNetCore.Mvc;
using miniui.Dto;
using Newtonsoft.Json;
using SqlSugar;

namespace miniui.Controllers
{
    public class DataGridController : Controller
    {
        private readonly ISqlSugarClient db;

        public DataGridController(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 列表查询+分页
        /// </summary>
        /// <param name="pageDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Index(PageDto pageDto)
        {
            RefAsync<int> total = 0;//REF和OUT不支持异步,想要真的异步这是最优解

            var list = await db.Queryable<t_employee>()
                .WhereIF(!string.IsNullOrEmpty(pageDto.key), m => m.Name.Contains(pageDto.key))
                .OrderByDescending(m => m.Createtime)
                .ToPageListAsync(pageDto.pageIndex, pageDto.pageSize, total);

            return Json(new
            {
                data = list,
                total = total.Value
            });
        }

        /// <summary>
        /// 查询部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDepartments()
        {
            var list = await db.Queryable<t_department>().ToListAsync();
            return Json(list);
        }

        /// <summary>
        /// 查询教育程度
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetEducationals()
        {
            var list = await db.Queryable<t_educational>().ToListAsync();
            return Json(list);
        }

        /// <summary>
        /// 按部门查询岗位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPositionsByDepartmenId(string id)
        {
            return Json(await db.Queryable<t_position>().Where(m => m.Dept_id == id).ToListAsync());
        }

        /// <summary>
        /// 添加/更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SaveAsync(model model)
        {
            if (!string.IsNullOrEmpty(model.data))
            {
                var emp = JsonConvert.DeserializeObject<t_employee[]>(model.data)?[0];

                //如果id不为空则是更新，并忽略创建时间列
                if (!string.IsNullOrEmpty(emp?.Id))
                {
                    await db.Updateable<t_employee>(emp).IgnoreColumns(m => m.Createtime).ExecuteCommandAsync();
                }
                else
                {
                    emp.Id = Guid.NewGuid().ToString();
                    emp.Createtime = DateTime.Now;
                    await db.Insertable<t_employee>(emp).ExecuteCommandAsync();
                }
            }

            return Json(new
            {
                Ok = true
            });
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetEmployee(string id)
        {
            return Ok(await db.Queryable<t_employee>().FirstAsync(m => m.Id == id));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> RemoveAsync(string id)
        {
            await db.Deleteable<t_employee>(id.Split(',')).ExecuteCommandAsync();
            return Ok(new { 
                msg = "ok"
            });
        }
    }

    public class model
    {
        public string data { get; set; }
    }
}
